package cn.yuanqiao.web.controller.archive.qzj.controller;

import cn.yuanqiao.archive.qzj.domain.YqdaQzjTree;
import cn.yuanqiao.archive.qzj.service.YqdaQzjTreeService;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/archive/qzj")
public class YqdaQzjTreeController extends BaseController {
    @Autowired
    private YqdaQzjTreeService qzjTreeService;

    @Log(title = "查询全宗卷列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public AjaxResult list(){
        return AjaxResult.success(qzjTreeService.QzjTree());
    }

    @Log(title = "获取全宗卷详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return AjaxResult.success(qzjTreeService.selectYqdaQzjTreeById(ID));
    }

    @Log(title = "添加全宗卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YqdaQzjTree qzjTree){
        if(StringUtils.isEmpty(qzjTree.getName())){
            throw new ServiceException("参数错误!");
        }
        return AjaxResult.success(qzjTreeService.insertYqdaQzjTree(qzjTree));
    }

    @Log(title = "修改全宗卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqdaQzjTree qzjTree){
        if(qzjTree.getId() == null){
            throw new ServiceException("参数错误!");
        }
        return AjaxResult.success(qzjTreeService.updateYqdaQzjTree(qzjTree));
    }

    @Log(title = "删除全宗卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/del")
    public AjaxResult remove(@RequestBody JSONObject ID)
    {
        String [] idsStr = ID.getString("ids").split(",");
        Long [] ids = new Long[idsStr.length];
        for(int i = 0;i<idsStr.length;i++){
            ids[i] = Long.valueOf(idsStr[i]);
        }
        return AjaxResult.success(qzjTreeService.delYqdaQzjTree(ids));
    }

}
